﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using QT.IO;

namespace FileSeacher
{
    public partial class Form1 : Form
    {
        #region Attributes

        private string IniPath = Application.StartupPath + @"\Setting.ini";
        string regexp = ".*";
        int sizetype = 0;
        int size = 0;
        Thread ThreadSeach = null;
        private delegate void DeleGateText(string txt);
        private delegate void DeleGateCancel(object sender, EventArgs e);
        private delegate void DeleGateGetLines(out int lines);

        #endregion

        #region Form

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.cboxType.SelectedIndex = 1;
            this.cboxUnit.SelectedIndex = 0;
            LoadSetting(IniPath);


        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            DialogResult result = MessageBox.Show("您确认要退出吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                SaveSetting(IniPath);
                if (ThreadSeach != null)
                {
                    if (ThreadSeach.IsAlive)
                    {
                        ThreadSeach.Abort();
                    }
                }
                e.Cancel = false;
            }
        }

        #endregion

        #region Button

        private void btnSelectDirectory_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.ShowNewFolderButton = false;
            this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            this.tbDirectory.Text = this.folderBrowserDialog1.SelectedPath;
        }

        private void btnSeach_Click(object sender, EventArgs e)
        {
            #region  验证输入字段合法性

            if (this.tbDirectory.Text.Length == 0)
            {
                MessageBox.Show("请设置搜索位置");
                return;
            }
            if (!Directory.Exists(this.tbDirectory.Text))
            {
                MessageBox.Show("目录不存在");
                return;
            }
            char[] chars = this.tbKeywords.Text.ToCharArray();
            foreach (char c in chars)
            {
                if (!CheckChar(c))
                {
                    MessageBox.Show("关键字不能包含下列任何字符：\n\t\\ / : \" < > |");
                    return;
                }
            }

            #endregion

            #region ControlHide

            this.ToolStripMenuItem_Open.Enabled = false;
            this.ToolStripMenuItem_Save.Enabled = false;
            this.ToolStripMenuItem_SaveAs.Enabled = false;
            this.toolStripMenuItem_Export.Enabled = false;

            this.tbDirectory.Enabled = false;
            this.btnSelectDirectory.Enabled = false;
            this.groupBox1.Enabled = false;

            this.btnCancel.Enabled = true;
            this.btnSeach.Enabled = false;

            #endregion

            #region InitSeach

            //关键字转正则表达式
            if (this.cbRegular.Checked)
            {
                regexp = this.tbKeywords.Text;
            }
            else
            {
                regexp = this.tbKeywords.Text;
                regexp = regexp.Replace("+", @"\+");
                regexp = regexp.Replace(".", @"\.");
                regexp = regexp.Replace("(", @"\(");
                regexp = regexp.Replace(")", @"\)");
                regexp = regexp.Replace("[", @"\[");
                regexp = regexp.Replace("]", @"\]");
                regexp = regexp.Replace("{", @"\{");
                regexp = regexp.Replace("}", @"\}");
                regexp = regexp.Replace("?", @".");
                regexp = regexp.Replace("*", @".*");
                if (!regexp.StartsWith("?") && !regexp.StartsWith("*") && !regexp.StartsWith(@".")) regexp = @".*" + regexp;
                if (!regexp.EndsWith("?") && !regexp.EndsWith("*") && !regexp.EndsWith(@".")) regexp += @".*";
                regexp = @"^" + regexp.ToUpper() + @"$";
            }
            //文件大小
            if (this.cbSize.Checked)
            {
                sizetype = this.cboxType.SelectedIndex;
                size = Convert.ToInt32(this.nudSize.Value);
                if (this.cboxUnit.SelectedIndex == 1) size *= 1024;
                if (this.cboxUnit.SelectedIndex == 2) size = size * 1024 * 1024;
                if (this.cboxUnit.SelectedIndex == 3) size = size * 1024 * 1024 * 1024;
            }
            //清空结果输出
            this.richTextBox1.Clear();

            #endregion

            ThreadSeach = new Thread(new ThreadStart(FileSeachStart));
            ThreadSeach.Start();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (ThreadSeach != null)
            {
                if (ThreadSeach.IsAlive)
                {
                    ThreadSeach.Abort();
                }
            }

            this.ToolStripMenuItem_Open.Enabled = true;
            this.ToolStripMenuItem_Save.Enabled = true;
            this.ToolStripMenuItem_SaveAs.Enabled = true;
            this.toolStripMenuItem_Export.Enabled = true;

            this.tbDirectory.Enabled = true;
            this.btnSelectDirectory.Enabled = true;
            this.groupBox1.Enabled = true;

            this.btnSeach.Enabled = true;
            this.btnCancel.Enabled = false;

            this.lblProgress.Text = "搜索已终止";
        }

        #endregion

        #region CheckBox

        private void cbKeywords_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbKeywords.Checked) this.tbKeywords.Enabled = true;
            else this.tbKeywords.Enabled = false;
        }

        private void cbDirectory_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbSize.Checked && this.cbDirectory.Checked)
            {
                MessageBox.Show("要匹配文件夹请先取消设置文件大小");
                this.cbDirectory.Checked = false;
                return;
            }
        }

        private void cbSize_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbDirectory.Checked && this.cbSize.Checked)
            {
                MessageBox.Show("文件大小设置只能用于匹配文件");
                this.cbSize.Checked = false;
                return;
            }
            if (this.cbSize.Checked)
            {
                this.cboxType.Enabled = true;
                this.nudSize.Enabled = true;
                this.cboxUnit.Enabled = true;
            }
            else
            {
                this.cboxType.Enabled = false;
                this.nudSize.Enabled = false;
                this.cboxUnit.Enabled = false;
            }
        }

        private void cbCreateTime_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbCreateTime.Checked)
            {
                this.dtpCreateDate_Start.Enabled = true;
                this.dtpCreateTime_Start.Enabled = true;
                this.dtpCreateDate_End.Enabled = true;
                this.dtpCreateTime_End.Enabled = true;
            }
            else
            {
                this.dtpCreateDate_Start.Enabled = false;
                this.dtpCreateTime_Start.Enabled = false;
                this.dtpCreateDate_End.Enabled = false;
                this.dtpCreateTime_End.Enabled = false;
            }
        }

        private void cbAccessTime_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbAccessTime.Checked)
            {
                this.dtpAccessDate_Start.Enabled = true;
                this.dtpAccessTime_Start.Enabled = true;
                this.dtpAccessDate_End.Enabled = true;
                this.dtpAccessTime_End.Enabled = true;
            }
            else
            {
                this.dtpAccessDate_Start.Enabled = false;
                this.dtpAccessTime_Start.Enabled = false;
                this.dtpAccessDate_End.Enabled = false;
                this.dtpAccessTime_End.Enabled = false;
            }
        }

        #endregion

        #region KeyEvent

        private void tbKeywords_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
            if (!this.cbRegular.Checked)
            {
                if (!CheckChar(e.KeyChar))
                {
                    MessageBox.Show("关键字不能包含下列任何字符：\n\t\\ / : \" < > |");
                    return;
                }
            }
            e.Handled = false;
        }

        #endregion

        #region ToolStripMenuItem

        /// <summary>
        /// 关于
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem_About_Click(object sender, EventArgs e)
        {
            new About().ShowDialog();
        }

        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem_Option_Click(object sender, EventArgs e)
        {
            MessageBox.Show("该功能暂不开发");
        }

        /// <summary> 
        /// 打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem_Open_Click(object sender, EventArgs e)
        {
            bool b = false;

            this.openFileDialog1.ValidateNames = true;
            this.openFileDialog1.Filter = "配置文件(*.ini)|*.ini";
            this.openFileDialog1.AddExtension = true;
            this.openFileDialog1.FilterIndex = 1;
            this.openFileDialog1.FileName = "Setting.ini";

            if (this.openFileDialog1.ShowDialog() == DialogResult.Cancel) return;
            else b = LoadSetting(this.openFileDialog1.FileName);

            if (b) MessageBox.Show("加载完成");
            else MessageBox.Show("加载失败");
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem_Save_Click(object sender, EventArgs e)
        {
            if (SaveSetting(this.IniPath)) MessageBox.Show("保存成功");
            else MessageBox.Show("保存失败");
        }

        /// <summary>
        /// 另存为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem_SaveAs_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.ValidateNames = true;
            this.saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|日志文件(*.log)|*.log";
            this.saveFileDialog1.AddExtension = true;
            this.saveFileDialog1.FilterIndex = 2;
            this.saveFileDialog1.DefaultExt = "log";
            this.saveFileDialog1.FileName = "SeachResult.log";

            if (this.saveFileDialog1.ShowDialog() == DialogResult.Cancel) return;
            else this.richTextBox1.SaveFile(this.saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);

            MessageBox.Show("保存成功");
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();//调用Form1_FormClosing
        }

        /// <summary>
        /// 结果导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_Export_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem_SaveAs_Click(sender, e);
        }

        #endregion

        #region Setting

        private bool LoadSetting(string Path)
        {
            INIFile iniFile = new INIFile(Path);
            string value = "";
            int number = 0;
            if (iniFile.ReadInivalue("Global", "Directory", out value)) this.tbDirectory.Text = value;
            if (iniFile.ReadInivalue("Keywords", "IsKeywords", out value))
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbKeywords.Checked = false;
                            break;
                        case 1:
                            this.cbKeywords.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Keywords", "Keywords", out value)) this.tbKeywords.Text = value;
            if(iniFile.ReadInivalue("Keywords","Regular",out value)) 
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbRegular.Checked = false;
                            break;
                        case 1:
                            this.cbRegular.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Type", "IsDirectory", out value))
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbDirectory.Checked = false;
                            break;
                        case 1:
                            this.cbDirectory.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Type", "IsFile", out value))
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbFile.Checked = false;
                            break;
                        case 1:
                            this.cbFile.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Type", "IsSub", out value))
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbSub.Checked = false;
                            break;
                        case 1:
                            this.cbSub.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Type", "IsHide", out value))
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbHide.Checked = false;
                            break;
                        case 1:
                            this.cbHide.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Size", "IsSize", out value))
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbSize.Checked = false;
                            break;
                        case 1:
                            this.cbSize.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Size", "Type", out value))
            {
                if(int.TryParse(value,out number)) this.cboxType.SelectedIndex=number;
            }
            if (iniFile.ReadInivalue("Size", "Size", out value))
            {
                if (int.TryParse(value, out number))
                {
                    if (number >= 0 && number < 1024)
                    {
                        this.nudSize.Value = number;
                    }
                    else
                    {
                        MessageBox.Show("数据异常");
                        return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Size", "Unit", out value))
            {
                if (int.TryParse(value, out number))
                {
                    this.cboxUnit.SelectedIndex = number;
                }
            }
            if (iniFile.ReadInivalue("Create", "IsCreateTime", out value))
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbCreateTime.Checked = false;
                            break;
                        case 1:
                            this.cbCreateTime.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Create", "CreateTimeStart", out value))
            {
                try
                {
                    DateTime dt = Convert.ToDateTime(value);
                    this.dtpCreateDate_Start.Value = dt;
                    this.dtpCreateDate_Start.Value = dt;
                }
                catch
                {
                    MessageBox.Show("数据异常");
                    return false;
                }
            }
            if (iniFile.ReadInivalue("Create", "CreateTimeEnd", out value))
            {
                try
                {
                    DateTime dt = Convert.ToDateTime(value);
                    this.dtpCreateDate_End.Value = dt;
                    this.dtpCreateTime_End.Value = dt;
                }
                catch
                {
                    MessageBox.Show("数据异常");
                    return false;
                }
            }
            if (iniFile.ReadInivalue("Access", "IsAccessTime", out value))
            {
                if (int.TryParse(value, out number))
                {
                    switch (number)
                    {
                        case 0:
                            this.cbAccessTime.Checked = false;
                            break;
                        case 1:
                            this.cbAccessTime.Checked = true;
                            break;
                        default:
                            MessageBox.Show("数据异常");
                            return false;
                    }
                }
            }
            if (iniFile.ReadInivalue("Access", "AccessTimeStart", out value))
            {
                try
                {
                    DateTime dt = Convert.ToDateTime(value);
                    this.dtpAccessDate_Start.Value = dt;
                    this.dtpAccessTime_Start.Value = dt;
                }
                catch
                {
                    MessageBox.Show("数据异常");
                    return false;
                }
            }
            if (iniFile.ReadInivalue("Access", "AccessTimeEnd", out value))
            {
                try
                {
                    DateTime dt = Convert.ToDateTime(value);
                    this.dtpAccessDate_End.Value = dt;
                    this.dtpAccessTime_End.Value = dt;
                }
                catch
                {
                    MessageBox.Show("数据异常");
                    return false;
                }
            }

            return true;
        }

        private bool SaveSetting(string Path)
        {
            try
            {
                INIFile iniFile = new INIFile(Path);
                iniFile.WriteInivalue("Global", "Directory", this.tbDirectory.Text);
                iniFile.WriteInivalue("Keywords", "IsKeywords", this.cbKeywords.Checked ? "1" : "0");
                iniFile.WriteInivalue("Keywords", "Keywords", this.tbKeywords.Text);
                iniFile.WriteInivalue("Keywords", "Regular", this.cbRegular.Checked ? "1" : "0");
                iniFile.WriteInivalue("Type", "IsDirectory", this.cbDirectory.Checked ? "1" : "0");
                iniFile.WriteInivalue("Type", "IsFile", this.cbFile.Checked ? "1" : "0");
                iniFile.WriteInivalue("Type", "IsSub", this.cbSub.Checked ? "1" : "0");
                iniFile.WriteInivalue("Type", "IsHide", this.cbHide.Checked ? "1" : "0");
                iniFile.WriteInivalue("Size", "IsSize", this.cbSize.Checked ? "1" : "0");
                iniFile.WriteInivalue("Size", "Type", this.cboxType.SelectedIndex.ToString());
                iniFile.WriteInivalue("Size", "Size", this.nudSize.Value.ToString());
                iniFile.WriteInivalue("Size", "Unit", this.cboxUnit.SelectedIndex.ToString());
                iniFile.WriteInivalue("Create", "IsCreateTime", this.cbCreateTime.Checked ? "1" : "0");
                iniFile.WriteInivalue("Create", "CreateTimeStart", new DateTime(this.dtpCreateDate_Start.Value.Year, this.dtpCreateDate_Start.Value.Month, this.dtpCreateDate_Start.Value.Day, this.dtpCreateTime_Start.Value.Hour, this.dtpCreateTime_Start.Value.Minute, this.dtpCreateTime_Start.Value.Second).ToString());
                iniFile.WriteInivalue("Create", "CreateTimeEnd", new DateTime(this.dtpCreateDate_End.Value.Year, this.dtpCreateDate_End.Value.Month, this.dtpCreateDate_End.Value.Day, this.dtpCreateTime_End.Value.Hour, this.dtpCreateTime_End.Value.Minute, this.dtpCreateTime_End.Value.Second).ToString());
                iniFile.WriteInivalue("Access", "IsAccessTime", this.cbAccessTime.Checked ? "1" : "0");
                iniFile.WriteInivalue("Access", "AccessTimeStart", new DateTime(this.dtpAccessDate_Start.Value.Year, this.dtpAccessDate_Start.Value.Month, this.dtpAccessDate_Start.Value.Day, this.dtpAccessTime_Start.Value.Hour, this.dtpAccessTime_Start.Value.Minute, this.dtpAccessTime_Start.Value.Second).ToString());
                iniFile.WriteInivalue("Access", "AccessTimeEnd", new DateTime(this.dtpAccessDate_End.Value.Year, this.dtpAccessDate_End.Value.Month, this.dtpAccessDate_End.Value.Day, this.dtpAccessTime_End.Value.Hour, this.dtpAccessTime_End.Value.Minute, this.dtpAccessTime_End.Value.Second).ToString());
            }
            catch
            {
                return false;
            }

            return true;
        }

        #endregion

        #region Seach

        private void FileSeachStart()
        {
            FileSeach(this.tbDirectory.Text);

            if (this.richTextBox1.Lines.Length == 0) this.richTextBox1.Invoke(new DeleGateText(outTxt), new object[] { "未找到与搜索条件匹配的项" });
            this.lblProgress.Invoke(new DeleGateText(outLabel), new object[] { "搜索完成,共找到" + (this.richTextBox1.Lines.Length - 1) + "条相关项."+DateTime.Now.ToString() });

            btnCancel.Invoke(new DeleGateCancel(btnCancel_Click), new object[] { new object(), new EventArgs() });
        }

        private void FileSeach(string Path)
        {
            #region Directory

            string[] Directories = null;

            #region 获取文件夹列表

            try
            {
                Directories = System.IO.Directory.GetDirectories(Path);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("目录地址包含非法字符");
                return;
            }
            catch (PathTooLongException)
            {
                MessageBox.Show("目录地址长度超过系统限制，最大248个字符");
                return;
            }
            catch (DirectoryNotFoundException)
            {
                MessageBox.Show("指定的目录地址无效");
                return;
            }
            catch (IOException)
            {
                MessageBox.Show("目录地址不能是文件，必须是文件夹");
                return;
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("权限不足，请确认您对该目录具有操作权限");
                return;
            }

            #endregion

            #region 搜索文件夹

            foreach (string Directory in Directories)
            {
                DirectoryInfo di = new DirectoryInfo(Directory);

                this.lblProgress.Invoke(new DeleGateText(outLabel), new object[] { di.FullName });

                if (this.cbDirectory.Checked)
                {
                    //隐藏属性
                    if (!this.cbHide.Checked)
                    {
                        if ((File.GetAttributes(di.FullName) & FileAttributes.Hidden) == FileAttributes.Hidden) goto NextLoop;
                    }
                    //关键字
                    if (this.cbKeywords.Checked)
                    {
                        if (!Regex.IsMatch(di.Name.ToUpper(), regexp)) goto NextLoop;
                    }
                    //大小
                    if (this.cbSize.Checked)
                    {
                        if (di.GetDirectories().Length == 0) goto NextLoop;
                    }
                    //创建时间
                    if (this.cbCreateTime.Checked)
                    {
                        DateTime StartTime = new DateTime(this.dtpCreateDate_Start.Value.Year, this.dtpCreateDate_Start.Value.Month, this.dtpCreateDate_Start.Value.Day, this.dtpCreateTime_Start.Value.Hour, this.dtpCreateTime_Start.Value.Minute, this.dtpCreateTime_Start.Value.Second);
                        DateTime EndTime = new DateTime(this.dtpCreateDate_End.Value.Year, this.dtpCreateDate_End.Value.Month, this.dtpCreateDate_End.Value.Day, this.dtpCreateTime_End.Value.Hour, this.dtpCreateTime_End.Value.Minute, this.dtpCreateTime_End.Value.Second);
                        if (di.CreationTime < StartTime || di.CreationTime > EndTime) goto NextLoop;
                    }
                    //修改时间
                    if (this.cbAccessTime.Checked)
                    {
                        DateTime StartTime = new DateTime(this.dtpAccessDate_Start.Value.Year, this.dtpAccessDate_Start.Value.Month, this.dtpAccessDate_Start.Value.Day, this.dtpAccessTime_Start.Value.Hour, this.dtpAccessTime_Start.Value.Minute, this.dtpAccessTime_Start.Value.Second);
                        DateTime EndTime = new DateTime(this.dtpAccessDate_End.Value.Year, this.dtpAccessDate_End.Value.Month, this.dtpAccessDate_End.Value.Day, this.dtpAccessTime_End.Value.Hour, this.dtpAccessTime_End.Value.Minute, this.dtpAccessTime_End.Value.Second);
                        if (di.LastWriteTime < StartTime || di.LastWriteTime > EndTime) continue;
                    }

                    this.richTextBox1.Invoke(new DeleGateText(outTxt), new object[] { di.FullName + ((File.GetAttributes(di.FullName) & FileAttributes.Hidden) == FileAttributes.Hidden ? " [Hide]" : "") + "\n" });
                }

            NextLoop:
                if (this.cbSub.Checked)
                {
                    FileSeach(Directory);
                }

            #endregion

            }

            #endregion

            #region File

            if (this.cbFile.Checked)
            {
                string[] Files = null;

                #region 获取文件列表

                try
                {
                    Files = System.IO.Directory.GetFiles(Path);
                }
                catch (ArgumentException)
                {
                    MessageBox.Show("目录地址包含非法字符");
                    return;
                }
                catch (PathTooLongException)
                {
                    MessageBox.Show("目录地址长度超过系统限制，最大248个字符");
                    return;
                }
                catch (DirectoryNotFoundException)
                {
                    MessageBox.Show("指定的目录地址无效");
                    return;
                }
                catch (IOException)
                {
                    MessageBox.Show("目录地址不能是文件，必须是文件夹");
                    return;
                }
                catch (UnauthorizedAccessException)
                {
                    MessageBox.Show("权限不足，请确认您对该目录具有操作权限");
                    return;
                }

                #endregion

                #region 搜索文件

                foreach (string File in Files)
                {
                    FileInfo fi = new FileInfo(File);
                    this.lblProgress.Invoke(new DeleGateText(outLabel), new object[] { fi.FullName });

                    //隐藏属性
                    if (!this.cbHide.Checked)
                    {
                        if ((System.IO.File.GetAttributes(fi.FullName) & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
                    }
                    //关键字
                    if (this.cbKeywords.Checked)
                    {
                        if (!Regex.IsMatch(fi.Name.ToUpper(), regexp)) continue;
                    }
                    //大小
                    if (this.cbSize.Checked)
                    {
                        if (sizetype == 0&&fi.Length < size) continue;
                        if (sizetype == 1 && fi.Length != size) continue;
                        if (sizetype == 2 && fi.Length > size) continue;
                    }
                    //创建时间
                    if (this.cbCreateTime.Checked)
                    {
                        DateTime StartTime = new DateTime(this.dtpCreateDate_Start.Value.Year, this.dtpCreateDate_Start.Value.Month, this.dtpCreateDate_Start.Value.Day, this.dtpCreateTime_Start.Value.Hour, this.dtpCreateTime_Start.Value.Minute, this.dtpCreateTime_Start.Value.Second);
                        DateTime EndTime = new DateTime(this.dtpCreateDate_End.Value.Year, this.dtpCreateDate_End.Value.Month, this.dtpCreateDate_End.Value.Day, this.dtpCreateTime_End.Value.Hour, this.dtpCreateTime_End.Value.Minute, this.dtpCreateTime_End.Value.Second);
                        if (fi.CreationTime < StartTime || fi.CreationTime > EndTime) continue;
                    }
                    //修改时间
                    if (this.cbAccessTime.Checked)
                    {
                        DateTime StartTime = new DateTime(this.dtpAccessDate_Start.Value.Year, this.dtpAccessDate_Start.Value.Month, this.dtpAccessDate_Start.Value.Day, this.dtpAccessTime_Start.Value.Hour, this.dtpAccessTime_Start.Value.Minute, this.dtpAccessTime_Start.Value.Second);
                        DateTime EndTime = new DateTime(this.dtpAccessDate_End.Value.Year, this.dtpAccessDate_End.Value.Month, this.dtpAccessDate_End.Value.Day, this.dtpAccessTime_End.Value.Hour, this.dtpAccessTime_End.Value.Minute, this.dtpAccessTime_End.Value.Second);
                        if (fi.LastWriteTime < StartTime || fi.LastWriteTime > EndTime) continue;
                    }

                    this.richTextBox1.Invoke(new DeleGateText(outTxt), new object[] { fi.FullName + ((System.IO.File.GetAttributes(fi.FullName) & FileAttributes.Hidden) == FileAttributes.Hidden ? " [Hide]" : " ") + "[" + fi.Length + "Bytes]\n" });
                }

                #endregion
            }

            #endregion
        }

        #endregion

        #region DeleGate

        private void outTxt(string text)
        {
            if (this.richTextBox1.TextLength + text.Length > this.richTextBox1.MaxLength)
            {
                this.richTextBox1.Clear();
            }
            this.richTextBox1.AppendText(text);
            this.richTextBox1.ScrollToCaret();
        }

        private void outLabel(string text)
        {
            this.lblProgress.Text = text;
        }

        private void GetLines(out int lines)
        {
            lines = this.richTextBox1.Lines.Length;
        }

        #endregion

        #region Method

        private bool CheckChar(char c)
        {
            if (c == '/' || c == '\\' || c == ':' || c == '"' || c == '<' || c == '>' || c == '|') return false;
            return true;
        }

        #endregion
    }
}
